﻿using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

namespace DevelopTool.Forms.FolderToolForms
{
    /// <summary>
    /// 根据文件扩展名提取文件名
    /// </summary>
    public partial class FileNameQueryByExtensionForm : BaseUIPage
    {
        public FileNameQueryByExtensionForm()
        {
            InitializeComponent();
        }

        private void FileNameQueryByExtension_Load(object sender,EventArgs e)
        {
        }


        /// <summary>
        /// 选择目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFolderPath_ButtonClick(object sender,EventArgs e)
        {
            string dir = txtFolderPath.Text;
            if (Dialogs.SelectDirEx("请选择目录", ref dir))
            {
                this.txtFolderPath.Text = dir;
            }
        }

        /// <summary>
        /// 提取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender,EventArgs e)
        {
            if(string.IsNullOrEmpty(this.txtFolderPath.Text))
            {
                this.ShowErrorNotifier("请选择或输入目录路径");
                return;
            }

            #region 核心

            //扩展名封装，在前面加上.
            string[] extensions = this.txtExtensions.Text.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries);
            for(int i = 0 ; i < extensions.Length ; i++)
            {
                if(!string.IsNullOrEmpty(extensions[i]) && extensions[i].IndexOf(".") != 0)
                {
                    extensions[i] = "." + extensions[i];
                }
            }


            IEnumerable<FileInfo> files = new List<FileInfo>();//文件集合

            LoadingHelper.ShowLoading("正在获取文件列表",() =>
            {
                if(this.chbContainSubFolder.Checked)
                {
                    //包含子目录
                    files = Directory.GetFiles(this.txtFolderPath.Text,"*.*",SearchOption.AllDirectories).Select(it => new FileInfo(it));
                }
                else
                {
                    //只查询当前目录
                    files = Directory.GetFiles(this.txtFolderPath.Text,"*.*",SearchOption.TopDirectoryOnly).Select(it => new FileInfo(it));
                }

                //如果输入了筛选扩展名
                if(extensions.Length > 0)
                    files = files.Where(it => extensions.Contains(it.Extension));//根据扩展名筛选


                //剔除没有扩展名的文件
                if(chbNotExtensionsRemove.Checked)
                    files = files.Where(it => !string.IsNullOrEmpty(it.Extension));//判断有没有扩展名
            });


            if(files.Count() <= 0)
            {
                this.codeEditorControl.ClearContent();
                this.ShowWarningNotifier("未找到任何相关文件");
                return;
            }

            //换行符
            string lineBreak = "\n";
            if(!this.chbWrap.Checked) lineBreak = string.Empty;

            IEnumerable<string> fileNames = new string[] { };

            if(this.chbFullPath.Checked) //完整文件路径
            {

                if(this.chbNotExtensions.Checked)
                {
                    fileNames = files.Where(it => !string.IsNullOrEmpty(it.Extension)).Select(it => it.FullName.Replace(it.Extension,string.Empty));//不包含扩展名
                }
                else
                {
                    fileNames = files.Select(it => it.FullName);//包含扩展名
                }
            }
            else//仅文件名
            {
                if(this.chbNotExtensions.Checked)
                {
                    fileNames = files.Where(it => !string.IsNullOrEmpty(it.Extension)).Select(it => it.Name.Replace(it.Extension,string.Empty));//不包含扩展名
                }
                else
                {
                    fileNames = files.Select(it => it.Name); //包含扩展名
                }
            }

            this.codeEditorControl.ContentText = string.Join(lineBreak,fileNames.Select(it => it + this.txtSeparator.Text));

            #endregion

        }


    }
}
